package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.common.enums.ShopProductReceiveStatusEnums;
import com.ytjj.qmyx.supplychain.mapper.ProductMapper;
import com.ytjj.qmyx.supplychain.mapper.ShopProductReceiveRecordMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProduct;
import com.ytjj.qmyx.supplychain.mapper.entity.YxShopProductReceiveRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ShopProductReceiveRecordService {
    @Resource
    private ShopProductReceiveRecordMapper shopProductReceiveRecordMapper;
    @Resource
    private ProductMapper productMapper;

    public boolean insertByBean(String shopId, List<Integer> productIds) {
        final Integer[] i = {0};
        productIds.stream().forEach(productId ->{
            Example example = new Example(YxShopProductReceiveRecord.class);
            example.createCriteria().andEqualTo("productId",productId).andEqualTo("shopId",shopId);
            example.setOrderByClause("id desc limit 1");
            List<YxShopProductReceiveRecord> yxShopProductReceiveRecords = shopProductReceiveRecordMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(yxShopProductReceiveRecords)) {
                YxShopProductReceiveRecord receiveRecord = new YxShopProductReceiveRecord();
                receiveRecord.setShopId(shopId);
                receiveRecord.setProductId(productId);
                receiveRecord.setCreateTime(new Date());
                receiveRecord.setStatus(ShopProductReceiveStatusEnums.ACTIVATION.getStatus());
                i[0] = i[0] + shopProductReceiveRecordMapper.insertSelective(receiveRecord);
            }else {
                YxShopProductReceiveRecord yxShopProductReceiveRecord = yxShopProductReceiveRecords.get(0);
                yxShopProductReceiveRecord.setStatus(1);
                i[0] = i[0] + shopProductReceiveRecordMapper.updateByPrimaryKey(yxShopProductReceiveRecord);
            }
        });

        return productIds.size() == i[0] ? true : false;
    }

    /**
     * 更新状态
     * @param productId 供应商商品id
     * @param shopId 店铺编号
     * @param status 状态 0-作废 1-正常
     * @return
     */
    public boolean updateStatus(Integer productId, String shopId, Integer status) {

        Example example = new Example(YxShopProductReceiveRecord.class);
        example.createCriteria().andEqualTo("productId",productId)
        .andEqualTo("shopId", shopId);
        List<YxShopProductReceiveRecord> list = shopProductReceiveRecordMapper.selectByExample(example);
        if (status.equals(ShopProductReceiveStatusEnums.UN_ACTIVATION.getStatus()) && CollectionUtils.isEmpty(list)) {
            return false;
        }
        YxShopProductReceiveRecord receiveRecord = list.get(0);
        if (receiveRecord.getStatus().equals(status)) {
            return true;
        }
        receiveRecord.setStatus(status);
        receiveRecord.setUpdateTime(new Date());
        return shopProductReceiveRecordMapper.updateByPrimaryKey(receiveRecord) > 0 ? true : false;
    }
}
